#include "AlgorytmB1.h"
#include "Skan3D.h"
#include <math.h>
namespace AlgorytmB1 {
    //------------------------------------------------------------------------------

    std::vector<Point*> *list = new std::vector<Point*>();
    std::vector<Para*> *kk = new std::vector<Para*>();

    void Main() {

        //  System.out.println("Wierzcho�ki\n");
        inclist();
        Analiza();
        for (unsigned int i = 0; i < list->size(); i++) {
            Point* p = list->at(i);
            if (p->w) p->druk();
        }
        //   System.out.println("end \n");
        return;
    }
    //------------------------------------------------------------------------------

    void wstawDane(Skan3D *sk) {

    }

    void Analiza() {

        int k = list->size();
        int k22 = k / 2;
        kk->push_back(new Para(0, k22)); //kk.add(new Para(0, k22));
        kk->push_back(new Para(k22, k - 1)); //kk.add(new Para(k22, k - 1));
        unsigned int i = 0;
        while (kk->size() > i) {
            //  System.out.println(kk.size()+"\n");
            int k1 = kk->at(i)->k1;
            int k2 = kk->at(i)->k2;
            line(k1, k2, 0.0, 0.0, 1.0);
            i++;
            //  kk.remove(0);

        }
    }
    //------------------------------------------------------------------------------

    bool line(int k1, int k2, double A, double B, double C) {
        double dl = list->size() - 1;
        if ((k1 < 0) || (k1 > dl) || (k1 < 0) || (k1 > dl)) return true;
        double x2 = list->at(k2)->x;
        double y2 = list->at(k2)->y;
        double z2 = list->at(k2)->z;
        double dx = list->at(k1)->x - list->at(k2)->x;
        double dy = list->at(k1)->y - list->at(k2)->y;
        double dz = list->at(k1)->z - list->at(k2)->z;
        double A1 = B * dz - C*dy;
        double B1 = A * dz - C*dx;
        double C1 = A * dy - B*dx;
        double D1 = -A1 * x2 - B1 * y2 - C1*z2;
        double S = sqrt(A1 * A1 + B1 * B1 + C1 * C1);
        A1 /= S;
        B1 /= S;
        C1 /= S;
        int maxind = k1;
        double max = 0.0;
        for (int i = k1; i < k2; i++) {
            Point* p = list->at(i);
            double dd = p->dist(A1, B1, C1, D1);
            if (dd > max) {
                maxind = i;
                max = dd;
            }
        }
        if (max > MAX_ERROR) {
            //      System.out.println("xxxxxxxxx "+max+"  "+maxind);
            kk->push_back(new Para(k1, maxind));
            kk->push_back(new Para(maxind, k2));
            return false;
        } else {
            (list->at(k1))->setw();
            (list->at(k2))->setw();
            // (list->at(k1))->druk();
            //(list->at(k2))->druk();
            return true;
        }
    }
    //------------------------------------------------------------------------------

    void inclist() {
        double dx = -2.0;
        double dy = -3.0;
        double x = 0;
        double y = 0;
        int i = 0;
        for (int j = 0; j < 10; j++) {
            x += dx;
            y += dy;
            //   System.out.println(x+"  "+y+"\n");
            //Point *p = new Point(i, j, x, y, 0.0);
            //   p.druk();
            list->push_back(new Point(i, j, x, y, 0.0));
        }
        dx = 2.0;
        dy = 0.0;
        for (int j = 0; j < 10; j++) {
            x += dx;
            y += dy;
            //   System.out.println(x+"  "+y+"\n");
            //Point* p = new Point(i, j, x, y, 0.0);
            //   p.druk();
            list->push_back(new Point(i, j, x, y, 0.0));
        }
        dx = 0.0;
        dy = 3.0;
        for (int j = 0; j < 10; j++) {
            x += dx;
            y += dy;
            //   System.out.println(x+"  "+y+"\n");
            //Point* p = new Point(i, j, x, y, 0.0);
            //   p.druk();
            list->push_back(new Point(i, j, x, y, 0.0));
        }

    }

}//namespace

